package cc.aiee.moneris.core.tool;

import JavaAPI.BankTotals;
import JavaAPI.Receipt;
import cc.aiee.core.kit.KitJson;
import cc.aiee.moneris.core.pojo.MonerisPayResponse;
import cn.hutool.core.util.StrUtil;

public class MonerisPayTool {

	public static MonerisPayResponse convert(Receipt receipt) {
		MonerisPayResponse response = new MonerisPayResponse();
		response.setCardType(receipt.getCardType());
		response.setTransAmount(receipt.getTransAmount());
		response.setTxnNumber(receipt.getTxnNumber());
		response.setReceiptId(receipt.getReceiptId());
		response.setTransType(receipt.getTransType());
		response.setReferenceNum(receipt.getReferenceNum());
		response.setResponseCode(receipt.getResponseCode());
		response.setIso(receipt.getISO());
		
		BankTotals bankTotals = receipt.getBankTotals();
		response.setBankTotals(KitJson.toJSONString(bankTotals));//JSON 数据格式
		
		response.setMessage(receipt.getMessage());
		response.setAuthCode(receipt.getAuthCode());
		response.setComplete(receipt.getComplete());
		response.setTransDate(receipt.getTransDate());
		response.setTransTime(receipt.getTransTime());
		response.setTicket(receipt.getTicket());
		response.setTimedOut(receipt.getTimedOut());
		response.setIsVisaDebit(receipt.getIsVisaDebit());
		response.setHostId(receipt.getHostId());
		response.setIssuerId(receipt.getIssuerId());

		return response;
	}

	/**
	 * 判断是否已审核
	 * <p>
	 * https://developer.moneris.com/More/Testing/Response%20Codes
	 * </p>
	 * 
	 * @param responseCode
	 * @return
	 * @author YangLong <br>
	 *         2020年11月9日
	 */
	public static boolean isApproved(String responseCode) {
		if (StrUtil.isBlank(responseCode)) {
			return false;
		}
		Integer code;
		try {
			code = Integer.parseInt(responseCode);
		} catch (NumberFormatException e) {
			return false;
		}
		return code == null ? false : code < 50;
	}

	public static void printf(Receipt receipt) {

		System.out.println("CardType = " + receipt.getCardType());
		System.out.println("TransAmount = " + receipt.getTransAmount());
		System.out.println("TxnNumber = " + receipt.getTxnNumber());
		System.out.println("ReceiptId = " + receipt.getReceiptId());
		System.out.println("TransType = " + receipt.getTransType());
		System.out.println("ReferenceNum = " + receipt.getReferenceNum());
		System.out.println("ResponseCode = " + receipt.getResponseCode());
		System.out.println("ISO = " + receipt.getISO());
		System.out.println("BankTotals = " + receipt.getBankTotals());
		System.out.println("Message = " + receipt.getMessage());
		System.out.println("AuthCode = " + receipt.getAuthCode());
		System.out.println("Complete = " + receipt.getComplete());
		System.out.println("TransDate = " + receipt.getTransDate());
		System.out.println("TransTime = " + receipt.getTransTime());
		System.out.println("Ticket = " + receipt.getTicket());
		System.out.println("TimedOut = " + receipt.getTimedOut());
		System.out.println("IsVisaDebit = " + receipt.getIsVisaDebit());
		System.out.println("HostId = " + receipt.getHostId());
		System.out.println("IssuerId = " + receipt.getIssuerId());
	}
}
